package com.app.newcsc.utils;

import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

import com.app.newcsc.app.AppApplication;


public class ToastUtils {
    private static Toast TOAST_SHORT;

    private static Toast TOAST_LONG;

    public static void toastShort(String message) {
        if (TOAST_SHORT == null) {
            TOAST_SHORT = Toast.makeText(AppApplication.getInstance().getApplicationContext()
                    , message, Toast.LENGTH_SHORT);
            TOAST_SHORT.show();
        } else {
            TOAST_SHORT.setText(message);
            TOAST_SHORT.show();
        }
    }

    public static void toastLong(String message) {
        if (TOAST_LONG == null) {
            TOAST_LONG = Toast.makeText(AppApplication.getInstance().getApplicationContext()
                    , message, Toast.LENGTH_LONG);
            TOAST_LONG.show();
        } else {
            TOAST_LONG.setText(message);
            TOAST_LONG.show();
        }
    }

    public static void showShort(String message) {
        if (TOAST_LONG == null) {
            TOAST_LONG = Toast.makeText(AppApplication.getInstance().getApplicationContext()
                    , message, Toast.LENGTH_SHORT);
            TOAST_LONG.show();
        } else {
            TOAST_LONG.setText(message);
            TOAST_LONG.show();
        }
    }
    public static void showShortSafe(String message) {
        if (TOAST_LONG == null) {
            TOAST_LONG = Toast.makeText(AppApplication.getInstance().getApplicationContext()
                    , message, Toast.LENGTH_SHORT);
            TOAST_LONG.show();
        } else {
            TOAST_LONG.setText(message);
            TOAST_LONG.show();
        }
    }

    private static Handler handler = new Handler(Looper.getMainLooper());

    public static void runInUiThread(Runnable task) {
        handler.post(task);
    }

    /**
     * 在子线程中弹吐司
     */
    public static void showToastOnThread( String content) {
        runInUiThread(new Runnable() {

            @Override
            public void run() {
                Toast.makeText(AppApplication.getInstance().getApplicationContext(), content, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
